package com.army.stock.service.impl;

import com.army.stock.dao.ArmyGoodDao;
import com.army.stock.domain.ArmyCategory;
import com.army.stock.domain.ArmyGood;
import com.army.stock.service.ArmyCategoryService;
import com.army.stock.service.ArmyGoodService;
import com.javalife.common.domain.page.PaginatedArrayList;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by zhangyong on 14/12/30.
 */
public class ArmyGoodServiceImpl implements ArmyGoodService {

    private ArmyCategoryService armyCategoryService;

    private ArmyGoodDao armyGoodDao;

    public void setArmyGoodDao(ArmyGoodDao armyGoodDao) {
        this.armyGoodDao = armyGoodDao;
    }

    public PaginatedArrayList getArmyGoodPageInfo(int currentPage, int pageSize, Map<String, Object> map) {
        int startNum = (currentPage - 1) * pageSize;
        map.put("start", startNum);
        map.put("pageSize", pageSize);
        Integer count = armyGoodDao.getCountArmyGoods(map);
        List<ArmyGood> Goods = armyGoodDao.getArmyGoods(map);
        if (CollectionUtils.isNotEmpty(Goods)) {
            for (ArmyGood armyGood : Goods) {
                ArmyCategory armyCategory = armyCategoryService.getArmyCategoryById(armyGood.getCategoryId());
                if (armyCategory != null) {
                    armyGood.setCategoryName(armyCategory.getCategoryName());
                }
            }
        }
        PaginatedArrayList<ArmyGood> paginatedArrayList = new PaginatedArrayList<ArmyGood>(currentPage, pageSize);
        paginatedArrayList.setTotalItem(count);
        paginatedArrayList.addAll(Goods);
        return paginatedArrayList;
    }

    public void addArmyGood(ArmyGood armyGood) {
        armyGoodDao.insertArmyGood(armyGood);
    }

    public void setArmyCategoryService(ArmyCategoryService armyCategoryService) {
        this.armyCategoryService = armyCategoryService;
    }

    public ArmyGood getArmyGoodById(Long id){
        return armyGoodDao.getArmyGoodById(id);
    }

    public ArmyGood getArmyGoodByGoodCode(String goodCode){
        return armyGoodDao.getArmyGoodByGoodCode(goodCode);
    }

    public void updateArmyGood(ArmyGood armyGood){
        armyGoodDao.updateArmyGood(armyGood);
    }

    public Integer delArmyGood(Long id){
       return armyGoodDao.delArmyGood(id);
    }

    public Integer getMaxGoodId() {
        return armyGoodDao.getMaxGoodId();
    }


}
